package cn.itcast.nems.journal.service.impl;

import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.journal.dto.JournalFinalLineDTO;
import cn.itcast.nems.journal.dto.JournalFinalLineQueryDTO;
import cn.itcast.nems.journal.service.JournalFinalLineService;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.studentrecord.dao.dataobject.JournalFinalLineDO;
import cn.itcast.nems.studentrecord.dao.entity.JournalFinalLine;
import cn.itcast.nems.studentrecord.dao.mapper.JournalFinalLineMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 结课凭单-行 服务实现类
 * </p>
 *
 * @author CodeGenerator
 * @since 2024-01-04
 */
@Service
public class JournalFinalLineServiceImpl extends ServiceImpl<JournalFinalLineMapper, JournalFinalLine> implements JournalFinalLineService {

    @Override
    public List<JournalFinalLineDTO> listByJournalFinalId(String journalFinalId) {
        List<JournalFinalLine> list = getBaseMapper().selectList(new LambdaQueryWrapper<JournalFinalLine>()
                .eq(JournalFinalLine::getJournalFinalId, journalFinalId)
                .eq(JournalFinalLine::getDeletedFlag, false)
        );
        // TODO 需要单独处理每一行学籍的剩余应付金额、已交金额。这两个字段前端显示随学籍变化。
        List<JournalFinalLineDTO> rst = null;
        if (!CollectionUtils.isEmpty(list)) {
            rst = list.stream().map(jfl -> {
                JournalFinalLineDTO jflt = new JournalFinalLineDTO();
                BeanUtils.copyProperties(jfl, jflt);
                return jflt;
            }).toList();
        }
        return rst;
    }

    @Override
    public IPage<JournalFinalLineDO> findPageList(PageQueryModel<JournalFinalLineQueryDTO> queryModel) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<JournalFinalLineDO> page =
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryModel.getPageNumber());
        page.setSize(queryModel.getPageSize());
        JournalFinalLineQueryDTO queryDTO = Objects.nonNull(queryModel.getCondition()) ?
                queryModel.getCondition() :
                new JournalFinalLineQueryDTO();
        JournalFinalLineDO lineDO = CustomBeanUtil.copyProperties(queryDTO, new JournalFinalLineDO());
        lineDO.setQueryStudentId(queryDTO.getStudentId());
        //        if (!AccountHolder.hasEmployeeGroups()) {
        //            return null;
        //        }
        //        lineDO.setEmployeeGroups(AccountHolder.getEmployeeGroups());
        return getBaseMapper().findPageList(page, lineDO);

    }

}
